package com.jwp.parser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.util.NodeList;

import com.jwp.post.PostAction;
import com.jwp.util.StringUtil;
import com.jwp.vo.Article;

/**
* @author Jeson
* @blog http://www.gbsou.com
* @date:Oct 9, 2009 1:08:10 PM
* @version :1.0
*
*/
public class CnBlogsParser {
    private static final int MAX_PAGE = 22;
    private static final int start_page = 1;
    private final String ENCODING = "UTF-8";
    public static String tag = "jquery";
    private PostAction post = new PostAction();
    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            for(int i=start_page;i<MAX_PAGE;i++){
            	System.out.println("------------------> page is "+i);
            	if(i%2 == 0){
            		Thread.sleep(3000L);
            	}
            new CnBlogsParser().testAttribute(i);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       

    }

    
    private void testAttribute(int pa) throws Exception{
        System.out.println("————开始解析页面："+pa);
        Parser p = new Parser();
        
        p.setURL("http://www.cnblogs.com/cate/"+tag+"/?page="+pa);
        p.setEncoding("UTF-8");
        
        NodeFilter filter =  new HasAttributeFilter("class","titlelnk");
        
        NodeList list = p.extractAllNodesThatMatch(filter);
        System.out.println(list.size());
        int cou = 0;
        for(int i=0 ; i<list.size();i++){
            
            String html = list.elementAt(i).toHtml(true);
            int [] index = {0,1,2};
            String [] bs = StringUtil.getStringsByRegex("<a class=\"titlelnk\" href=\"(.*)\" target=\"_blank\">(.*)</a>", index, html);
            String title = bs[2];
            String url = bs[1];
            System.out.println(url);
            
            String content = getContent(bs[1]);
            
            if(content == null || "".equals(content)){
                continue;
            }
            Article art = new Article();
            art.setTitle(title);
            art.setBody("<p><!--more--></p>"+content);
            art.setLink(url);
            //createFile(art);
            post.postArticle(art);
            cou++;
        }
        System.out.println("over"+cou);
    }
    
    private String getContent(String url) throws Exception{
        Parser p = new Parser();
        
        p.setURL(url);
        p.setEncoding(ENCODING);
        NodeFilter filter =  new HasAttributeFilter("class","post");
        
        NodeList list = p.extractAllNodesThatMatch(filter);
        String a = list.toHtml();
        return a;
        
    }
        
    private void createFile(Article art){
        try {
            File d = new File("d:\\\\cnblog");
            if(!d.exists()){
                d.mkdir();
            }
            File f = new File("d:\\\\cnblog\\\\"+art.getTitle()+".html");
            if(!f.exists()){
                f.createNewFile();
                System.out.println("——–>"+art.getTitle()+"文件已经创建");
            }
            
            OutputStream file = new FileOutputStream(f.getPath());

            file.write(art.getBody().getBytes());
            file.flush();
            file.close();
            System.out.println("文件写入完毕，地址"+f.getPath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(art.getLink()+" "+art.getTitle()+"文件写入失败");
        }
    }
   

}
